// hooks/useCommunicationSkillSummary.js
import {useEffect, useState} from 'react';
import {hostAddr} from '@/config.js';

export function useCommunicationSkillSummary(chatId) {
  const [summary, setSummary] = useState('');
  const [error, setError] = useState(null);
  const [connected, setConnected] = useState(false);

  useEffect(() => {
    if (!chatId) return;

    const url = `${hostAddr}/communication-skill/summary/${chatId}`;
    const eventSource = new EventSource(url);

    eventSource.onmessage = (event) => {
      setSummary(event.data);
    };

    eventSource.onopen = () => {
      setConnected(true);
    };

    eventSource.onerror = (err) => {
      console.error('SSE 错误:', err);
      setError(err);
      eventSource.close();
    };

    return () => {
      eventSource.close();
    };
  }, [chatId]);

  return {summary, error, connected};
}